package ie.deloitte.cvapp.dataaccess;

import ie.deloitte.cvapp.domain.Skills;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository("skillDAO")
@Transactional
public class SkillDAOImpl implements SkillDAO {

	@Autowired
	private SessionFactory sessionFactory;

	private Session currentSession() {
		return this.sessionFactory.getCurrentSession();
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void save(Skills sk) {
		currentSession().saveOrUpdate(sk);
	}

	public Skills getById(String id) {
		// TODO Auto-generated method stub
		Query namedQuery = currentSession().getNamedQuery(
				"Skills.findByIdSkills");
		namedQuery.setParameter("idSkill", Integer.valueOf(id));
		List list = namedQuery.list();
		Skills sk = new Skills();
		if (!list.isEmpty()) {
			sk = (Skills) list.get(0);
		}
		return sk;
		// return null;
	}

	public void update(Skills sk) {
		// TODO Auto-generated method stub
		currentSession().update(sk);
	}

	public List<Skills> getAll() {

		Query namedQuery = currentSession().getNamedQuery("Skills.findAll");
		List list = namedQuery.list();
		return list;

	}

}